package com.gdiot.smart_scale;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class Login_Fragment extends Fragment {

    TextInputLayout loginInputUsername;
    TextInputLayout loginInputPassword;
    MaterialButton loginOk;
    String appkey = "7992bedadd9805f702701fe85df86a53";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.login_fragment, container, false);
        loginOk = view.findViewById(R.id.login_ok);
        loginInputUsername = view.findViewById(R.id.login_input_username);
        loginInputPassword = view.findViewById(R.id.login_input_password);
        Bmob.initialize(getActivity(),appkey);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        loginOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = loginInputUsername.getEditText().getText().toString();
                String userPassword = loginInputPassword.getEditText().getText().toString();

                if (userName!=null && userName.length()!=0){
                    if (userPassword!=null && userPassword.length()!=0){
                        //连接Bmob进行条件查询，判断用户名是否存在
                        //如果不存在，弹出Toast提示用户名不存在
                        //如果存在，继续判断，将查询出的密码和输入框中密码进行匹配
                        //若不相同，则弹出提示，密码验证失败
                        //若相同，验证成功。
                        BmobQuery<manager> managerBmobQuery = new BmobQuery<>();
                        managerBmobQuery.addWhereEqualTo("managerName" , userName);
                        managerBmobQuery.findObjects(new FindListener<manager>() {
                            @Override
                            public void done(List<manager> list, BmobException e) {
                                if( e == null ){
                                    String getPW = list.get(0).getManagerPassword().toString();
                                    if (getPW.equals(userPassword)){
                                         // 验证成功，进行跳转。进入销售界面
                                        Toast.makeText(getActivity(), "验证成功", Toast.LENGTH_SHORT).show();
                                        Intent intent = new Intent(getActivity() , SellActivity.class);
                                        startActivity(intent);
                                    }else {
                                        //密码错误，弹出提示
                                        Toast.makeText(getActivity(), "密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                                    }
                                }else{
                                    Toast.makeText(getActivity(), "用户名不存在" + e.getMessage().toString(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                    else {
                        Toast.makeText(getActivity(), "密码不能为空", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(getActivity(), "用户名不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
